// packageJ/consensus_activity/asset_withdraw/asset_withdraw.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    count: 0,
    CategoryShow:false,
    type: null,//1-转入加权分红额度、2-转入质积分、3-转入消费积分、4-转入元积分提现配额、5-会员转让
    radio:"",
    category_list:[],
    name:"",

    dividend_min: 0,
    custom_name: {},
    reopen: 0,
    reopen_to_add_weight_dividend_rate: 0,
    reopen_to_consume_fee_rate:0,
    reopen_to_yuan_withdraw_fee_rate:0,

    origin_to_consume_fee_rate:"",
    origin_to_yuan_withdraw_fee_rate:"",
    default_name:"",
    default_amount:"",
    origin_asset_name:"",
    member_id:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let type = Number(options.num);
    this.setData({type,name:options.name})
    let text = '';
    if (type == 1) {
      text = '转入加权分红额度';
    } else if (type == 2) {
      text = '转入质积分';
    } else if (type == 3) {
      text = '转入消费积分';
    } else if (type == 4) {
      text = '转入元积分提现配额';
    } else {
      text = '会员转让';
    }
    wx.setNavigationBarTitle({
      title: text,
    })
    this.getData();
  },
  InputHandle(e){
    this.setData({
      member_id:e.detail.value
    })
  },
  onClick(event) {
    const { name } = event.currentTarget.dataset;
    this.setData({
      radio: name,
    });
  },
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  showPopup(){
    if(this.data.name == 'asset'){
      this.setData({
        CategoryShow:true
      })
    }
  },
  submit(){
    let urlStr = "",type=this.data.type;
    if (this.data.name == 'reopen') {
      if (type == 1) {
        urlStr = 'plugin.wise-yuan-trade.frontend.reopen-value.to-add-weight';
      } else if (type == 2) {
        urlStr = `plugin.wise-yuan-trade.frontend.reopen-value.to-quality`;
      } else if (type == 3) {
        urlStr = 'plugin.wise-yuan-trade.frontend.reopen-value.to-integral-point';
      } else if (type == 4) {
        urlStr = 'plugin.wise-yuan-trade.frontend.reopen-value.to-yuan-quota';
      } else if (type == 6) {
        urlStr = 'plugin.wise-yuan-trade.frontend.reopen-value.to-point';
      }

    } else {

      if (type == 1) {
        urlStr = 'plugin.wise-yuan-trade.frontend.origin-asset.to-add-weight';
      } else if (type == 2) {
        urlStr = `plugin.wise-yuan-trade.frontend.origin-asset.to-quality`;
      } else if (type == 3) {
        urlStr = 'plugin.wise-yuan-trade.frontend.origin-asset.to-integral-point';
      } else if (type == 4) {
        urlStr = 'plugin.wise-yuan-trade.frontend.origin-asset.to-yuan-quota';
      } else {
        urlStr = 'plugin.wise-yuan-trade.frontend.origin-asset.transfer';
      }
    }

    let json = {amount:Number(this.data.count)};
    if(this.data.name == 'asset'){
      json.asset_id = this.data.radio;
      if(this.data.type == 5){
        json.uid = this.data.member_id;
      }
    }

    app._postNetWork({
      url:app.getNetAddresss(urlStr),
      data:json,
      success:resdata=>{
        let {result,msg} = resdata.data;
        app.tips(msg);
        if(result){
          this.setData({count:0})
          this.getData();
        }
      }
    })


  },
  getData(){
    let urlStr = "plugin.wise-yuan-trade.frontend.origin-asset.index";
    if (this.data.name == 'reopen') {
      urlStr = "plugin.wise-yuan-trade.frontend.reopen-value.index"
    }
    app._getNetWork({
      url:app.getNetAddresss(urlStr),
      success:resdata=>{
        let {result,msg,data} = resdata.data;
        if(result){
          this.setData({
            custom_name : data.custom_name,
          })
          if(this.data.name == 'asset'){

            this.setData({
             category_list:data.origin_asset_list,
             origin_asset_name:data.custom_name.origin_asset_name,
             origin_to_consume_fee_rate: data.origin_to_consume_fee_rate,
             origin_to_yuan_withdraw_fee_rate: data.origin_to_yuan_withdraw_fee_rate
            })

            if(!this.data.radio){
              this.setData({
                radio: data.origin_asset_list.length!==0 && data.origin_asset_list[0].asset_id || '',
                default_name:data.origin_asset_list.length!==0 && data.origin_asset_list[0].origin_asset.name ||'',
                default_amount:data.origin_asset_list.length!==0 && data.origin_asset_list[0].amount || 0,
              })
            }else{
              let index =data.origin_asset_list.findIndex(item=>item.asset_id == this.data.radio);
              this.setData({
                default_name:data.origin_asset_list.length!==0 && data.origin_asset_list[index].origin_asset.name ||'',
                default_amount:data.origin_asset_list.length!==0 && data.origin_asset_list[index].amount || 0,
              })
            }
          }else{
            this.setData({
              dividend_min:data.reopen_to_add_weight_dividend_min,
              reopen : data.reopen,
              reopen_to_add_weight_dividend_rate : data.reopen_to_add_weight_dividend_rate,
              reopen_to_consume_fee_rate : data.reopen_to_consume_fee_rate,
              reopen_to_yuan_withdraw_fee_rate:data.reopen_to_yuan_withdraw_fee_rate
            })
          }
          let text = '',type=this.data.type;
          if (type == 1) {
            text = '转入加权分红额度';
          } else if (type == 2) {
            text = `转入${data.custom_name.quality_name}`;
          } else if (type == 3) {
            text = `转入${data.integral_name || data.custom_name.integral_name}`;
          } else if (type == 4) {
            text = `转入${data.custom_name.yuan_withdraw_limit_name}`;
          } else if (type == 6) {
            text = `转入${data.point_name}`;
          } else {
            text = '会员转让';
          }
          wx.setNavigationBarTitle({
            title: text,
          })
        }else{
          app.tips(msg);
        }
      }
    })

  },
  onInput(e){
    this.setData({count:e.detail.value})
  },
  confirmCategory(){
    let index = this.data.category_list.findIndex(item=>item.asset_id==this.data.radio);
    console.log(this.data.category_list[index],"index",index);
    let default_name = this.data.category_list[index].origin_asset.name;
    let default_amount = this.data.category_list[index].amount;
    this.setData({
      CategoryShow:false,
      default_name,default_amount
    })
  },
  onFocus(e){
    if(Number(e.detail.value)==0){
      this.setData({count:''})
    }
  },
  all_handle() {
    if(this.name=='reopen'){
      this.setData({count:this.data.reopen})
    }else{
      this.setData({count:Number(this.data.default_amount)})
    }
  },
  onClose(){
    this.setData({CategoryShow:false})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})